package com.cssy.xxkj.basic.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.byd.acegiext.domain.User;
import com.cssy.xxkj.attach.dao.AttachFileUploadDAO;
import com.cssy.xxkj.basic.dao.BasicMaterialsDAO;
import com.cssy.xxkj.basic.dao.BasicMaterialsTypeDAO;
import com.cssy.xxkj.basic.dao.BasicStapleUnitDAO;
import com.cssy.xxkj.basic.entity.BasicMaterialsDO;
import com.cssy.xxkj.basic.entity.BasicMaterialsTypeDO;
import com.cssy.xxkj.basic.entity.BasicStapleUnitDO;
import com.cssy.xxkj.basic.service.IBasicMaterialsService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.yuendong.sy.exception.SyCodeException;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;


@Service("basicMaterialsService")
public class BasicMaterialsServiceImpl implements IBasicMaterialsService{
	@Resource(name="syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name="basicMaterialsDAO")	
	private BasicMaterialsDAO basicMaterialsDAO;
	//基本单位DAO
	@Resource(name="basicStapleUnitDAO")	
	private BasicStapleUnitDAO basicStapleUnitDAO;
	//物料类型DAO
	@Resource(name="basicMaterialsTypeDAO")	
	private BasicMaterialsTypeDAO basicMaterialsTypeDAO;
	//附件
	@Resource(name = "attachFileUploadDAO")
	private AttachFileUploadDAO  attachFileUploadDAO;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<BasicMaterialsDO>  list=basicMaterialsDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			Map<String,BasicStapleUnitDO> unitMap = this.stapleUnitMap();
			Map<String,BasicMaterialsTypeDO> maTypeMap=this.materialsTypeMap();
			for(int i=0;i<list.size();i++){
				if(list.get(i).getZd007Unit()!=null&&!"".equals(list.get(i).getZd007Unit())){
					if(unitMap!=null){
						list.get(i).setBasicStapleUnitDO(unitMap.get(list.get(i).getZd007Unit()));
					}
				}
				if(list.get(i).getZd029MaterTypeId()!=null&&!"".equals(list.get(i).getZd029MaterTypeId())){
					if(maTypeMap!=null){
						list.get(i).setBasicMaterialsTypeDO(maTypeMap.get(list.get(i).getZd029MaterTypeId()));
					}
				}
			}
		}
		rs.setRecordCount(basicMaterialsDAO.count(scs));
		rs.setResults(list);
		return rs;
	}
	
	@Override
	public SearchResult findExBomListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<BasicMaterialsDO> list=basicMaterialsDAO.findExBom(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			Map<String,BasicStapleUnitDO> unitMap = this.stapleUnitMap();
			Map<String,BasicMaterialsTypeDO> maTypeMap=this.materialsTypeMap();
			for(int i=0;i<list.size();i++){
				if(list.get(i).getZd007Unit()!=null&&!"".equals(list.get(i).getZd007Unit())){
					if(unitMap!=null){
						list.get(i).setBasicStapleUnitDO(unitMap.get(list.get(i).getZd007Unit()));
					}
				}
				if(list.get(i).getZd029MaterTypeId()!=null&&!"".equals(list.get(i).getZd029MaterTypeId())){
					if(maTypeMap!=null){
						list.get(i).setBasicMaterialsTypeDO(maTypeMap.get(list.get(i).getZd029MaterTypeId()));
					}
				}
			}
		}
		rs.setRecordCount(basicMaterialsDAO.countExBom(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return basicMaterialsDAO.deleteBasicMaterialsDOByPrimaryKey(id);
	}

	@Override
	public Integer update(BasicMaterialsDO t, User user) throws SyBaseException {
		t.setBizModifyName(user.getName());
		t.setBizModifyDate(new Date());
		t.setBizVersionMark(t.getBizVersion());
		t.setBizVersion(t.getBizVersion()+1);
		return basicMaterialsDAO.updateBasicMaterialsDO(t);
	}

	@Override
	public BasicMaterialsDO save(BasicMaterialsDO t, User user)
			throws SyBaseException {
		try{
			String id  = IDCodeTypeUtil.getId();
			t.setZd001Code(syCodeService.getCode("IM","IM",SyCode.DATE_YYMM,5));
			t.setId(id);
			t.setBizCreatorDate(new Date());
			t.setBizCreatorName(user.getName());
			t.setBizVersion(1);
			basicMaterialsDAO.insertBasicMaterialsDO(t);
			return t;
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
	}
	
	@Override
	public void saveAttach(BasicMaterialsDO curAP, HttpServletRequest request)
			throws SyBaseException{
		if(curAP==null||StringUtils.isBlank(curAP.getId())){
			return;
		}
		attachFileUploadDAO.updateFileRelated(curAP.getZd035FileUppath(),curAP.getId(),"com.cssy.xxkj.basic.entity.BasicMaterialsDO","ATTACH_MATERIALS");
	}

	@Override
	public BasicMaterialsDO findListById(String id) throws SyBaseException {
		return basicMaterialsDAO.findBasicMaterialsDOByPrimaryKey(id);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return basicMaterialsDAO.deleteBizRemoved(id);
	}
	
	/**
	 * 返回基本单位MAP
	 * @return
	 */
	private Map<String,BasicStapleUnitDO> stapleUnitMap(){
		Map<String,BasicStapleUnitDO> map = new HashMap<String, BasicStapleUnitDO>();
		SearchConditions scs = new SearchConditions();
		scs.setRemovedProperty(null);
		List<BasicStapleUnitDO>  list=basicStapleUnitDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
				map.put(list.get(i).getId(), list.get(i));
			}
		}else{
			return null;
		}
		return map;
	}
	
	/**
	 * 返回物料类型MAP
	 * @return
	 */
	private Map<String,BasicMaterialsTypeDO> materialsTypeMap(){
		Map<String,BasicMaterialsTypeDO> map = new HashMap<String, BasicMaterialsTypeDO>();
		SearchConditions scs = new SearchConditions();
		scs.setRemovedProperty(null);
		List<BasicMaterialsTypeDO>  list=basicMaterialsTypeDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
				map.put(list.get(i).getId(), list.get(i));
			}
		}else{
			return null;
		}
		return map;
	}
}
